/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package core.test;

import core.net.DropboxRequest;
import core.net.Json;
import core.net.OnedriveRequest;
import core.oauth.ServiceFactory;
import core.pojos.DropboxDir;
import core.pojos.DropboxItem;
import core.pojos.OneDriveDir;
import core.pojos.OneDriveItem;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URISyntaxException;
import java.util.Scanner;
import static org.scribe.model.OAuthConstants.EMPTY_TOKEN;
import org.scribe.model.Token;
import org.scribe.model.Verifier;
import org.scribe.oauth.OAuthService;

/**
 *
 * @author vudn
 */
public class OnedriveApiExample {
    public static void main(String[] args) throws IOException, URISyntaxException {
        OAuthService service = ServiceFactory.create(ServiceFactory.ONEDRIVE);

        Scanner in = new Scanner(System.in);
        System.out.println("=== Onedrive's OAuth Workflow ===");
        System.out.println();

        System.out.println("Fetching the Request Token...");
        String authorizationUrl = service.getAuthorizationUrl(EMPTY_TOKEN);
        System.out.println("Got the Authorization URL!");
        System.out.println(authorizationUrl);
        System.out.println("And paste the verifier here");
        System.out.print(">>");
        Verifier verifier = new Verifier(in.nextLine());
        System.out.println();

        System.out.println("Trading the Request Token for an Access Token...");
        Token token = service.getAccessToken(EMPTY_TOKEN, verifier);
        System.out.println("Got the Access Token!");
        System.out.println("(if your curious it looks like this: " + token + " )");
        System.out.println();

        OnedriveRequest request = new OnedriveRequest(token);
        System.out.println("");
        System.out.println("Listing content of root directory ...");
        String json = request.list("folder.3ea87296076047ff");
//        String json = request.list("");
        // deserialize json string to list DropBoxItem
        OneDriveDir dir = Json.ToOneDriveDir(json);
        for (OneDriveItem item : dir.data) {
            System.out.println(item.name + " "+ item.id +" " +item.type);
        }
        
        
        System.out.println("Downloading... file b.txt");
        File target = new File("src/main/resources/c.txt");
        OutputStream out = new FileOutputStream(target);
        try {
            request.download("file.3ea87296076047ff.3EA87296076047FF!672", out);
        } catch (Exception e) {
            out.close();
        }
        
        

        
        System.out.println("Done!!!");
    }
}
